var graphInstanceMap = new Object();

function adfDvtInitGraph(componentId) {
  // Find the parent div of the component
  var parentId = componentId + "_container";

  // Create the SVG Document and add it to the DOM
  var chartDiv = document.getElementById(parentId);
  var svg = DvtSvgUtils.createSvgDocument();
  chartDiv.appendChild(svg);

  // Create the component and pass in the rendering context.  The null fields on the
  // component are placeholders for eventing callbacks.  The interactivity sample will
  // demonstrate their usage.

  var aeh = new ApplicationEventHandler(componentId);
  var context = new DvtSvgContext(svg);
  var graph = new DvtStandaloneGraph(context, componentId, aeh.handleEvent, aeh);
  // Add the component to the display tree of the rendering context.
  context.getStage().addChild(graph);

  // Set the XML to render
  graph.setXML(adfDvtGetXml(componentId));

  svg.addEventListener('click', function (event) {
                                  var graphCtx = graph.getGraphContext(event.target);
                                  if (graphCtx.getUserProps())
                                  alert("Custom Property: " + graphCtx.getUserProps());
                                  }, false);
  // Store the graph instance for updates later
  graphInstanceMap[componentId] = graph;
}

function adfDvtUpdateGraph(componentId, xml) {
  var graph = graphInstanceMap[componentId];
  if(graph) 
    graph.setXML(xml);
}

function adfDvtGetXml(componentId) {
  var graphXml = document.getElementById(componentId + '_xml');
  var xmlText = graphXml.innerHTML;

  var index1 = xmlText.indexOf('<!--');
  var index2 = xmlText.lastIndexOf('-->');
  xmlText = xmlText.substring(index1 + 4, index2);
  return xmlText;
}

function ajaxCallback(sourceId, destId, url, params) {
  var xmlhttp = false;

  if (window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
  else if (window.ActiveXObject)
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

  xmlhttp.open("POST", url, true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.onreadystatechange = function () {

    if (xmlhttp.readyState == 4) {
      // Server response
      if (xmlhttp.responseText != "") {
        // Parse the XML from the response
        var response = xmlhttp.responseText;
        var index1 = response.indexOf('<!--');
        var index2 = response.lastIndexOf('-->');
        var xml = response.substring(index1 + 4, index2);
      
        // Set the xml on the component
        adfDvtUpdateGraph(destId, xml);
      }
    }
  }

  // Convert the payload to a string so that we can send it up
  var payload = "";
  for (var name in params) {
    payload += name;
    payload += "~;";
    payload += params[name];
    payload += "~;";
  }
  
  var graph = graphInstanceMap[sourceId];
  var clientProperties = "";
  if (url == "selection") 
    clientProperties = graph.getClientProperties();
  xmlhttp.send("id=" + sourceId + "&payload=" + payload + "&clientPropertiesPayload=" + clientProperties);
}

function zoomAndScrollCallback(componentId, params) {
  ajaxCallback(componentId, componentId, "zoomAndScroll", params);
}

function timeSelectorCallback(componentId, params) {
  ajaxCallback(componentId, "detailGraph", "timeSelector", params);
}

// Used for Flash only
function selectionDemoEventCallback(componentId, params) {
  ajaxCallback(componentId, componentId, "selection", params);
}

function customPropertycallback(componentId, params) {
    var graph = graphInstanceMap[componentId];
}

var ApplicationEventHandler = function(id) {
    this.id = id;
}

ApplicationEventHandler.prototype.handleEvent = function(event) {

    var type = event.getType();
    if (type == DvtSelectionEvent.TYPE)
      ajaxCallback(this.id, this.id, "selection", null);
}

// TODO Add test code here
function testGraph(componentId) {
    var graph = graphInstanceMap[componentId];
    var seriesCount=graph.getSeriesCount();
    var groupCount=graph.getGroupCount();
    var graphTitle=graph.getTitle();
    var legendTitle=graph.getLegend().getTitle();
    var O1Title = graph.getO1Axis().getTitle();
    var y1Title = graph.getY1Axis().getTitle(); 
  graph.getAutomation().click("data[1][1]");
  alert("Series Count: " +seriesCount+"\nGroup Count: "+groupCount + "\nGraph Title: "+ graphTitle+ 
  "\nLegend Title: "+ legendTitle + "\nO1Axis Title: "+ O1Title + "\ny1Axis Title: "+ y1Title);
}

function testGraph(componentId) {
    var graph = graphInstanceMap[componentId];
    var seriesCount=graph.getSeriesCount();
    var groupCount=graph.getGroupCount();
    var graphTitle=graph.getTitle();
    var legendTitle=graph.getLegend().getTitle();
    var O1Title = graph.getO1Axis().getTitle();
    var y1Title = graph.getY1Axis().getTitle(); 

  alert("Series Count: " +seriesCount+"\nGroup Count: "+groupCount + "\nGraph Title: "+ graphTitle+ 
  "\nLegend Title: "+ legendTitle + "\nO1Axis Title: "+ O1Title + "\ny1Axis Title: "+ y1Title);
}

function clickGraph(componentId) {
    var graph = graphInstanceMap[componentId];
  graph.getAutomation().click("data[0][1]");
}
